package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created on 2023/6/27
 */
@Component
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private AdminRoleService adminRoleService;

    //根据条件查询分页
    @Override
    public IPage<Role> selectPageVo(Page<Role> pageParm, RoleQueryVo roleQueryVo) {

        //创建Mp的Lambda条件构造器
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //获取角色名称
        String roleName = roleQueryVo.getRoleName();

        //判断有没有角色名称条件
        if (!StringUtils.isEmpty(roleName)) {
            lambdaQueryWrapper.like(Role::getRoleName, roleName);
        }

        //调用方法实现分页查询
        IPage<Role> selectPage = baseMapper.selectPage(pageParm,lambdaQueryWrapper);

        return selectPage;
    }

    /**
     * 获取用户角色
     * @param adminId
     * @return
     */
    @Override
    public Map<String,Object> toAssignByAdminId(Long adminId) {

        //1、获取所有的角色
        List<Role> allRolesList = baseMapper.selectList(null);

        //2、获取用户分配的角色
        //2.1、根据用户Id获取用户分配角色的对象
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);

        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);

        //2.2、使用stream流获取用户分配角色对象的ID
        List<Long> roleByAdminId = adminRoleList.
                stream().map(item -> item.getRoleId()).collect(Collectors.toList());

        //2.3、创建List集合封装用户分配的角色对象
        List<Role> assignRoles = new ArrayList<Role>();

        //2.4、遍历所有的角色，判断分配的角色是否包含在角色表中

        for (Role role : allRolesList) {
            if (roleByAdminId.contains(role.getId())) {
                assignRoles.add(role);
            }
        }
            //3、把所有角色和用户分配的角色封装到Map集合中,返回
            Map<String, Object> roleMap = new HashMap<>();
            //用户所有角色
            roleMap.put("assignRoles", assignRoles);
            //用户分配的角色
            roleMap.put("allRolesList", allRolesList);

            return roleMap;

    }

    /**
     *为用户分配角色
     */
    @Override
    public List<AdminRole> adminToRole(Long adminId, Long[] roleId) {

        //1、先根据传入的用户Id删除用户分配的角色
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        adminRoleService.remove(wrapper);

        //2、获取前端传入的用户Id和多个角色ID，遍历roleId，然后一个一个对应的把用户分配的角色添加
        //2.1、创建一个集合，封装用户分配的角色，方面一次性分配多个角色
        List<AdminRole> list = new ArrayList<>();
        for(Long adminRoleId : roleId){
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(adminRoleId);
            list.add(adminRole);
        }
        //2.3、给用户分配角色
        adminRoleService.saveBatch(list);

        return list;
    }
}
